﻿using System;

namespace HoNSharp
{
    class GenericRecvBuffer
    {
        protected string _buffer;
        protected int _pos;

        public GenericRecvBuffer()
        {
            this._pos = 0;
            this._buffer = "";
        }

        public void Add(byte[] data)
        {
            this._buffer += this.byteArrayToString(data, data.Length);
        }

        public void Add(byte[] data, int length)
        {
            this._buffer += this.byteArrayToString(data, length);
        }

        public string GetBuffer()
        {
            return this._buffer;
        }

        public string GetApparentBuffer()
        {
            return this._buffer.Substring(this._pos);
        }

        protected string byteArrayToString(byte[] data, int length)
        {
            char[] strOut = new char[length];
            for (int i = 0; i < length; i++)
            {
                strOut[i] = Convert.ToChar(data[i]);
            }
            string ret = new string(strOut);
            return ret;
        }

        protected byte[] stringToByteArray(string data)
        {
            byte[] arrOut = new byte[data.Length];
            for (int i = 0; i < data.Length; i++)
            {
                arrOut[i] = Convert.ToByte(data[i]);
            }
            return arrOut;
        }
    }
}
